﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Drawing;
using System.ComponentModel;
using System.Data;
using System.Text;
using BL;
using DAL;
using System.Xml;
using System.Xml.Linq;


namespace WebsiteBanHang
{
    public partial class About : System.Web.UI.Page
    {
        NhaSanXuatBL nhaSanXuat = new NhaSanXuatBL();
        DanhMucLoaiSanPhamBL dmloaiSanPham = new DanhMucLoaiSanPhamBL();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                LayDSNhaSanXuat();
                LayDMLoaiSanPham();
            }
        }        

        protected void LayDSNhaSanXuat()
        {
            List<NhaSanXuatDAL> nsx = new List<NhaSanXuatDAL>();
            nsx = nhaSanXuat.LayDanhSachNhaSanXuat();

            if (!IsPostBack)
            {                
                ddlnhaSanXuat.DataSource = nsx;
                ddlnhaSanXuat.DataValueField = "MaNhaSanXuat";
                ddlnhaSanXuat.DataTextField = "TenNhaSanXuat";
                ddlnhaSanXuat.DataBind();
            }
            //for (int i = 0; i < nsx.Count; ++i)
            //{
            //    ddlnhaSanXuat.Items.Add(nsx[i].TenNhaSanXuat);
            //    ddlnhaSanXuat.Items[i].Value = nsx[i].MaNhaSanXuat;
            //    ddlnhaSanXuat.Items[i].Text = nsx[i].TenNhaSanXuat;
            //}
        }

        protected void LayDMLoaiSanPham()
        {
            List<DanhMucLoaiSanPhamDAL> dmlsp = new List<DanhMucLoaiSanPhamDAL>();
            dmlsp = dmloaiSanPham.LayDanhMucLoaiSanPham();

            if (!IsPostBack)
            {
                ddlloaiSanPham.DataSource = dmlsp;
                ddlloaiSanPham.DataValueField = "MaLoaiSanPham";
                ddlloaiSanPham.DataTextField = "TenLoaiSanPham";
                ddlloaiSanPham.DataBind();
            }

            DanhMucLoaiSanPhamDAL lsp = new DanhMucLoaiSanPhamDAL();
            lsp = dmloaiSanPham.LayLoaiSPTheoMaLoai(ddlloaiSanPham.SelectedValue);

            XmlDocument doc = new XmlDocument();
            doc.LoadXml(lsp.ThongTinRieng);

            XmlNodeList danhSachNut = doc.SelectNodes("/THONGSOKYTHUAT/THONGSO");
            Repeater1.DataSource = danhSachNut;
            Repeater1.DataBind();
        }

        protected string WriteXMLToString()
        {
            XmlDocument doc = new XmlDocument();
            //XmlElement nutCha = (CreateElement("THONGSOKYTHUAT")).ParentNode;
            XmlNode nutCha = doc.CreateElement("THONGSOKYTHUAT");

            for (int i = 0; i < Repeater1.Items.Count; ++i)
            {
                XmlNode nut = doc.CreateElement("THONGSO");

                //XmlAttribute THONGSO_Attribute = doc.CreateAttribute("ThongSo");
                Label lb1 = (Label)Repeater1.Items[i].FindControl("Label10");
                Label lb2 = (Label)Repeater1.Items[i].FindControl("Label11");
                TextBox txtbox = (TextBox)Repeater1.Items[i].FindControl("TextBox5");
   
                XmlAttribute ThongSo_Attribute = doc.CreateAttribute("ThongSo");
                ThongSo_Attribute.Value = lb1.Text;
                nut.Attributes.Append(ThongSo_Attribute);

                XmlAttribute DacTinh_Attribute = doc.CreateAttribute("DacTinh");
                DacTinh_Attribute.Value = lb2.Text;
                nut.Attributes.Append(DacTinh_Attribute);

                XmlAttribute GiaTri_Attribute = doc.CreateAttribute("GiaTri");
                GiaTri_Attribute.Value = txtbox.Text;
                nut.Attributes.Append(GiaTri_Attribute);

                
                nutCha.AppendChild(nut);
                //string a = nut.OuterXml;
            }
            //string tskt = nutCha.InnerXml;
            return (string)nutCha.OuterXml;
        }

        protected void Button2_Click(object sender, EventArgs e)
        {            
            SanPhamDAL sp = new SanPhamDAL();
            sp.MaSanPham = TextBox1.Text;
            sp.TenSanPham = TextBox2.Text;
            sp.HinhAnh = "../images/" + TextBox6.Text;

            sp.MaNhaSanXuat = ddlnhaSanXuat.SelectedValue;
            sp.MaLoaiSanPham = ddlloaiSanPham.SelectedValue;

            sp.GiaBan = float.Parse(TextBox3.Text);
            sp.GiaMua = 0;
            sp.PhanTramKM = float.Parse(RadioButtonList1.SelectedValue);
            sp.SoLuongTon = int.Parse(TextBox4.Text);
            //sp.ThongSoKyThuat = WriteXMLToString();   
            sp.ThongSoKyThuat = WriteXMLToString();
            sp.ThongTinMoTa = TextBox7.Text;

            sp.TinhTrang = true;
            sp.SanPhamMoi = true;
            SanPhamBL sanPham = new SanPhamBL();
            sanPham.ThemSanPhamMoi(sp);

            TextBox1.Text = "";
            TextBox2.Text = "";
            ddlnhaSanXuat.SelectedIndex = 0;
            ddlloaiSanPham.SelectedIndex = 0;
            TextBox3.Text = "";
            TextBox4.Text = "";
            TextBox6.Text = "";
            TextBox7.Text = "";        
            
            RadioButtonList1.SelectedIndex = -1;
            Image1.ImageUrl = "../images/NOimage.jpg";
        }

        public string TextBox5
        {
            get { return TextBox5; }
            set { TextBox5 = value; }
        }

        protected void ddlloaiSanPham_SelectedIndexChanged(object sender, EventArgs e)
        {
            DanhMucLoaiSanPhamDAL lsp = new DanhMucLoaiSanPhamDAL();
            lsp = dmloaiSanPham.LayLoaiSPTheoMaLoai(ddlloaiSanPham.SelectedValue);

            // Create the XmlDocument.
            XmlDocument doc = new XmlDocument();
            doc.LoadXml(lsp.ThongTinRieng);

            XmlNodeList danhSachNut = doc.SelectNodes("/THONGSOKYTHUAT/THONGSO");
            Repeater1.DataSource = danhSachNut;
            Repeater1.DataBind();
        }

        protected void LinkButton1_Click(object sender, EventArgs e)
        {
            TextBox6.Text = FileUpload1.FileName;
            Image1.ImageUrl = "../images/" + TextBox6.Text;
        }

        protected void Repeater1_ItemCommand(object source, RepeaterCommandEventArgs e)
        {

        }

        protected void Repeater1_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType != ListItemType.Item && e.Item.ItemType !=
      ListItemType.AlternatingItem)
                return;
            TextBox TextBox5 = e.Item.FindControl("TextBox5") as TextBox;
            if (TextBox5 != null)
            {
                TextBox5.Text = "";
            } 
        }
       
    }
}
